package user_profile

import (
	"context"

	"algo-sdk-go/common/tools"
	"algo-sdk-go/project/matchmaking/application/command"
	"algo-sdk-go/project/matchmaking/common/constant"
	"algo-sdk-go/project/matchmaking/matchMaking-api/internal/svc"
	"algo-sdk-go/project/matchmaking/matchMaking-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SyncUserprofileLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSyncUserprofileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncUserprofileLogic {
	return &SyncUserprofileLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SyncUserprofileLogic) SyncUserprofile(req *types.SyncUserprofileRequest) (resp *types.SyncUserprofileResponse, err error) {
	userNum, err := l.svcCtx.UserProfileService.AddUserProfile(l.ctx, TransSyncUserprofileCommand(req))
	if err != nil {
		return nil, err
	}

	resp = &types.SyncUserprofileResponse{
		Data: types.SyncUserprofileData{
			UserNum: userNum,
		},
	}
	return
}

func TransSyncUserprofileCommand(req *types.SyncUserprofileRequest) command.UserProfileCommand {
	return command.UserProfileCommand{
		UserNum:       req.UserNum,
		UserAvatar:    req.UserAvatar,
		Name:          req.Name,
		Gender:        constant.Gender(req.Gender),
		BirthDate:     req.BirthDate,
		Height:        req.Height,
		Weight:        req.Weight,
		MaritalStatus: constant.MaritalStatus(req.MaritalStatus),
		Education:     constant.Education(req.Education),
		Assets: constant.AssetInfo{
			AnnualIncome: req.Assets.AnnualIncome,
			Car:          constant.AssetStatus(req.Assets.Car),
			House:        constant.AssetStatus(req.Assets.House),
		},
		IsPartyMember:    req.IsPartyMember,
		Occupation:       (constant.Occupation)(req.Occupation),
		Hometown:         req.Hometown,
		CurrentResidence: req.CurrentResidence,
		FamilyInfo: constant.FamilyInfo{
			FamilyType: constant.FamilyType(req.FamilyMembers.FamilyType),
			FatherDetail: constant.RelationDetail{
				Age:  req.FamilyMembers.FatherDetail.Age,
				Desc: req.FamilyMembers.FatherDetail.Desc,
			},
			MotherDetail: constant.RelationDetail{
				Age:  req.FamilyMembers.MotherDetail.Age,
				Desc: req.FamilyMembers.MotherDetail.Desc,
			},
			OtherDetail: tools.Cast(req.FamilyMembers.OtherDetail, func(t types.RelationDetail) constant.RelationDetail {
				return constant.RelationDetail{
					Age:  t.Age,
					Desc: t.Desc,
				}
			}),
		},
		Status:     "",
		Verified:   constant.VerifiedUser,
		CreateUser: "",
		ModifyUser: "",
	}
}
